package cn.smbms.controller;

import cn.smbms.pojo.User;
import cn.smbms.service.UserService;
import cn.smbms.tools.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录控制器
 *
 * @author Kane
 * @create 2019--28 18:16
 */
@Controller
public class LoginController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Autowired
    private UserService userService;
    @RequestMapping("/login.html")
    public String loigin(){
        logger.info("login====");
        return "login";
    }
    @RequestMapping("/dologin.html")
    public String doLogin(@RequestParam String userCode,
                          @RequestParam String userPassword,
                          HttpServletRequest request, HttpSession session){
        User user = userService.findLoginUser(userCode,userPassword);
        if(user != null){
            session.setAttribute(Constants.USER_SESSION,user);
            return "redirect:/sys/main.html";
        }else{
            request.setAttribute("error","用户名或者密码有误!!!");
            return "login";
        }
    }
    @RequestMapping("/sys/main.html")
    public String main(){
        return "frame";
    }
    @RequestMapping("/logout.html")
    public String logout(HttpSession session){
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }
}
